package com.jjwxc.jwjskandriod.config;

import android.os.Handler;
import android.os.Looper;

import com.jjwxc.jwjskandriod.framework.base.FFApplication;
import com.jjwxc.jwjskandriod.framework.config.FFConfig;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;
import com.jjwxc.jwjskandriod.readActivity.utils.UMConstant;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

import java.io.InputStream;

public class YtApplication extends FFApplication {

    public static YtApplication app;
    private static Handler handler;

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        init();
        //设置LOG开关，默认为false
        UMConfigure.setLogEnabled(true);
        //友盟预初始化

        try {
            InputStream is = getAssets().open("channel.txt");
            byte[] input = new byte[is.available()];
            is.read(input);
            String channel = new String(input, "UTF-8").trim();
            UMConfigure.preInit(getApplicationContext(), UMConstant.APP_KEY, channel);

            if (FFConfig.DEBUG) {
                showToast(channel);
            }
        } catch (Exception e) {
            UMConfigure.preInit(getApplicationContext(), UMConstant.APP_KEY, UMConstant.CHANNEL);
        }

        /**
         * 打开app首次隐私协议授权，以及sdk初始化，判断逻辑请查看SplashTestActivity
         */
        //判断是否同意隐私协议，uminit为1时为已经同意，直接初始化umsdk
        if (PreUtils.getBoolean("XIE_YI", false)) {
            //友盟正式初始化
            UMConfigure.init(getApplicationContext(), UMConstant.APP_KEY, UMConstant.CHANNEL, UMConfigure.DEVICE_TYPE_PHONE,
                    "");
            MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);
        }
    }

    public static YtApplication getInstance() {
        return app;
    }

    public void init() {
        handler = new Handler(Looper.getMainLooper());
    }

    public static void removeCallbacks(Runnable task) {
        handler.removeCallbacks(task);
    }

    public static void post(Runnable task) {
        handler.post(task);
    }

    public static void postDelay(Runnable task, long delay) {
        handler.postDelayed(task, delay);
    }


}
